package com.hjm.project.agentInfo.agentActActive.utils;

import com.hjm.common.exception.BusinessException;
import com.hjm.project.agentInfo.agentPolicy.domain.AgentPolicy;
import lombok.Data;

import javax.validation.constraints.Digits;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Data
public class AgentPriceCommon {
    private String agentNum;                //代理商编号
    private Integer posType;                //产品类型 0 传统 4 电签
    @NotBlank(message = "政策编号不可为空")
    private String actId;                  //政策编号

    /********刷卡借记卡*************/
    @Digits(integer = 1,fraction = 4,message = "刷卡借记卡结算格式错误")
    private BigDecimal s0_shuakaJiejika;
    private Integer s0_shuakaJiejikaFd;
    @Min(value = 0,message = "刷卡借记卡附加手续费最低0元")
    @Max(value = 3,message = "刷卡借记卡附加手续费最高3元")
    private Integer s0_shuakaJiejikaFj;

    /********刷卡贷记卡*************/
    @Digits(integer = 1,fraction = 4,message = "刷卡贷记卡格式错误")
    private BigDecimal s0_shuakaDaijika;
    @Min(value = 0,message = "刷卡贷记卡附加手续费最低0元")
    @Max(value = 3,message = "刷卡贷记卡附加手续费最高3元")
    private Integer s0_shuakaDaijikaFj;

    /********银联二维码借记卡*************/
    @Digits(integer = 1,fraction = 4,message = "银联二维码借记卡结算格式错误")
    private BigDecimal s0_qrJiejika;
    private Integer s0_qrJiejikaFd;
    @Min(value = 0,message = "银联二维码借记卡附加手续费最低0元")
    @Max(value = 3,message = "银联二维码借记卡附加手续费最高5元")
    private Integer s0_qrJiejikaFj;

    /********银联二维码贷记卡*************/
    @Digits(integer = 1,fraction = 4,message = "银联二维码贷记卡格式错误")
    private BigDecimal s0_qrDaijika;
    @Min(value = 0,message = "银联二维码贷记卡附加手续费最低0元")
    @Max(value = 3,message = "银联二维码贷记卡附加手续费最高5元")
    private Integer s0_qrDaijikaFj;


    /********云闪付*************/
    @Digits(integer = 1,fraction = 4,message = "云闪付格式错误")
    private BigDecimal yunshanfu0;
    @Min(value = 0,message = "云闪付附加手续费最低0元")
    @Max(value = 3,message = "云闪付附加手续费最高5元")
    private Integer yunshanfu0Fj;

    /********微信*************/
    @Digits(integer = 1,fraction = 4,message = "微信格式错误")
    private BigDecimal saoma0Wx;
    @Min(value = 0,message = "微信附加手续费最低0元")
    @Max(value = 3,message = "微信附加手续费最高5元")
    private Integer saoma0FjWx;


    /********支付宝*************/
    @Digits(integer = 1,fraction = 4,message = "支付宝格式错误")
    private BigDecimal saoma0Zfb;
    @Min(value = 0,message = "支付宝附加手续费最低0元")
    @Max(value = 3,message = "支付宝附加手续费最高5元")
    private Integer saoma0FjZfb;





    public static List<AgentPolicy> viewToBean(AgentPriceCommon agentPriceVo){
        List<AgentPolicy> agentPriceList = new ArrayList<>();

        //刷卡贷记卡
        AgentPolicy shuakaDaijika = new AgentPolicy();
        shuakaDaijika.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        shuakaDaijika.setPayType(0);
        //结算 %
        shuakaDaijika.setPrice(agentPriceVo.getS0_shuakaDaijika());
        //结算封顶结算 笔/元
        shuakaDaijika.setTopCost(0);
        //附加单笔结算 笔/元
        shuakaDaijika.setFjCost(agentPriceVo.getS0_shuakaDaijikaFj());
        //卡类型 1 借记卡 2 贷记卡
        shuakaDaijika.setCardType(2);
        agentPriceList.add(shuakaDaijika);

        //刷卡借记卡
        AgentPolicy shuakaJiejika = new AgentPolicy();
        shuakaJiejika.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        shuakaJiejika.setPayType(0);
        //结算 %
        shuakaJiejika.setPrice(agentPriceVo.getS0_shuakaJiejika());
        //结算封顶结算 笔/元
        shuakaJiejika.setTopCost(agentPriceVo.getS0_shuakaJiejikaFd());
        //附加单笔结算 笔/元
        shuakaJiejika.setFjCost(agentPriceVo.getS0_shuakaJiejikaFj());
        //卡类型 1 借记卡 2 贷记卡
        shuakaJiejika.setCardType(1);
        agentPriceList.add(shuakaJiejika);



        //银联二维码贷记卡
        AgentPolicy qrDaijika = new AgentPolicy();
        qrDaijika.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        qrDaijika.setPayType(4);
        //结算 %
        qrDaijika.setPrice(agentPriceVo.getS0_qrDaijika());
        //结算封顶结算 笔/元
        qrDaijika.setTopCost(0);
        //附加单笔结算 笔/元
        qrDaijika.setFjCost(agentPriceVo.getS0_qrDaijikaFj());
        //卡类型 1 借记卡 2 贷记卡
        qrDaijika.setCardType(2);
        agentPriceList.add(qrDaijika);

        //银联二维码借记卡
        AgentPolicy qrJiejika = new AgentPolicy();
        qrJiejika.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        qrJiejika.setPayType(4);
        //结算 %
        qrJiejika.setPrice(agentPriceVo.getS0_qrJiejika());
        //结算封顶结算 笔/元
        qrJiejika.setTopCost(agentPriceVo.getS0_qrJiejikaFd());
        //附加单笔结算 笔/元
        qrJiejika.setFjCost(agentPriceVo.getS0_qrJiejikaFj());
        //卡类型 1 借记卡 2 贷记卡
        qrJiejika.setCardType(1);
        agentPriceList.add(qrJiejika);



        //云闪付
        AgentPolicy yunshanfu = new AgentPolicy();
        yunshanfu.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        yunshanfu.setPayType(1);
        yunshanfu.setCardType(2);
        //结算 %
        yunshanfu.setPrice(agentPriceVo.getYunshanfu0());
        //结算封顶结算 笔/元
        yunshanfu.setTopCost(0);
        //附加单笔结算 笔/元
        yunshanfu.setFjCost(agentPriceVo.getYunshanfu0Fj());
        agentPriceList.add(yunshanfu);

        //微信
        AgentPolicy saomaoWx = new AgentPolicy();
        saomaoWx.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        saomaoWx.setPayType(2);
        saomaoWx.setCardType(2);
        //结算 %
        saomaoWx.setPrice(agentPriceVo.getSaoma0Wx());
        //结算封顶结算 笔/元
        saomaoWx.setTopCost(0);
        //附加单笔结算 笔/元
        saomaoWx.setFjCost(agentPriceVo.getSaoma0FjWx());
        agentPriceList.add(saomaoWx);


        //支付宝
        AgentPolicy saomaoZfb = new AgentPolicy();
        saomaoZfb.setAgentNum(agentPriceVo.getAgentNum());
        //交易类型 0 银行卡收单 1 云闪付 2-微信 3-支付宝 4-银联二维码
        saomaoZfb.setPayType(3);
        saomaoZfb.setCardType(2);
        //结算 %
        saomaoZfb.setPrice(agentPriceVo.getSaoma0Zfb());
        //结算封顶结算 笔/元
        saomaoZfb.setTopCost(0);
        //附加单笔结算 笔/元
        saomaoZfb.setFjCost(agentPriceVo.getSaoma0FjZfb());
        agentPriceList.add(saomaoZfb);
        for (AgentPolicy agentPolicy : agentPriceList) {
            agentPolicy.setActId(agentPriceVo.getActId());
            agentPolicy.setPrice(agentPolicy.getPrice().divide(new BigDecimal("100")));
        }
        return agentPriceList;
    }

    public static BigDecimal appendStr(BigDecimal str){
        String plainString = str.toPlainString();
        if (plainString.length() == 3) {
            plainString = plainString + "0";
            return new BigDecimal(plainString);
        }
        return str;
    }

    public static AgentPriceCommon dbToView(List<AgentPolicy> agentPolicies){
        AgentPriceCommon policyInfoVo = new AgentPriceCommon();
        for (AgentPolicy agentPolicy : agentPolicies) {
            BigDecimal price = agentPolicy.getPrice().multiply(new BigDecimal("100")).stripTrailingZeros();
            price = appendStr(price);
            if (agentPolicy.getPayType() == 0 && agentPolicy.getCardType() == 2){ //刷卡贷记卡
                policyInfoVo.setS0_shuakaDaijika(price);
                policyInfoVo.setS0_shuakaDaijikaFj(agentPolicy.getFjCost());
            }

            if (agentPolicy.getPayType() == 0 && agentPolicy.getCardType() == 1){ //刷卡借记卡
                policyInfoVo.setS0_shuakaJiejika(price);
                policyInfoVo.setS0_shuakaJiejikaFd(agentPolicy.getTopCost());
                policyInfoVo.setS0_shuakaJiejikaFj(agentPolicy.getFjCost());
            }

            if (agentPolicy.getPayType() == 4 && agentPolicy.getCardType() == 2){ //银联二维码贷记卡
                policyInfoVo.setS0_qrDaijika(price);
                policyInfoVo.setS0_qrDaijikaFj(agentPolicy.getFjCost());
            }

            if (agentPolicy.getPayType() == 4 && agentPolicy.getCardType() == 1){ //银联二维码借记卡
                policyInfoVo.setS0_qrJiejika(price);
                policyInfoVo.setS0_qrJiejikaFd(agentPolicy.getTopCost());
                policyInfoVo.setS0_qrJiejikaFj(agentPolicy.getFjCost());
            }

            if (agentPolicy.getPayType() == 1){ //云闪付
                policyInfoVo.setYunshanfu0(price);
                policyInfoVo.setYunshanfu0Fj(agentPolicy.getFjCost());
            }
            if (agentPolicy.getPayType() == 2){ //微信
                policyInfoVo.setSaoma0Wx(price);
                policyInfoVo.setSaoma0FjWx(agentPolicy.getFjCost());
            }

            if (agentPolicy.getPayType() == 3){ //支付宝
                policyInfoVo.setSaoma0Zfb(price);
                policyInfoVo.setSaoma0FjZfb(agentPolicy.getFjCost());
            }
        }
        return policyInfoVo;
    }

    public static void valiAgentPrice(AgentPriceCommon targetPrice,AgentPriceCommon belongPrice){

        //刷卡借记卡结算
        if (targetPrice.getS0_shuakaJiejika().compareTo(belongPrice.getS0_shuakaJiejika()) == -1) {
            throw new BusinessException("刷卡借记卡结算不可小于上级 " + belongPrice.getS0_shuakaJiejika());
        }
        //刷卡借记卡封顶
        if (targetPrice.getS0_shuakaJiejikaFd().compareTo(belongPrice.getS0_shuakaJiejikaFd()) == -1) {
            throw new BusinessException("刷卡借记卡封顶不可小于上级 " + belongPrice.getS0_shuakaJiejikaFd());
        }
        //刷卡借记卡附加
        if (targetPrice.getS0_shuakaJiejikaFj().compareTo(belongPrice.getS0_shuakaJiejikaFj()) == -1) {
            throw new BusinessException("刷卡借记卡附加手续费不可小于上级 " + belongPrice.getS0_shuakaJiejikaFj() + "元");
        }


        //刷卡贷记卡结算
        if (targetPrice.getS0_shuakaDaijika().compareTo(belongPrice.getS0_shuakaDaijika()) == -1) {
            throw new BusinessException("刷卡贷记卡结算不可小于上级 " + belongPrice.getS0_shuakaDaijika());
        }
        //刷卡贷记卡附加手续费
        if (targetPrice.getS0_shuakaDaijikaFj().compareTo(belongPrice.getS0_shuakaDaijikaFj()) == -1) {
            throw new BusinessException("刷卡贷记卡附加手续费不可小于上级 " + belongPrice.getS0_shuakaDaijikaFj() + "元");
        }

        //银联二维码借记卡结算
        if (targetPrice.getS0_qrJiejika().compareTo(belongPrice.getS0_qrJiejika()) == -1) {
            throw new BusinessException("银联二维码借记卡结算不可小于上级 " + belongPrice.getS0_qrJiejika());
        }
        //银联二维码借记卡封顶
        if (targetPrice.getS0_qrJiejikaFd().compareTo(belongPrice.getS0_qrJiejikaFd()) == -1) {
            throw new BusinessException("银联二维码借记卡封顶不可小于上级 " + belongPrice.getS0_qrJiejikaFd());
        }
        //银联二维码借记卡附加
        if (targetPrice.getS0_qrJiejikaFj().compareTo(belongPrice.getS0_qrJiejikaFj()) == -1) {
            throw new BusinessException("银联二维码借记卡附加手续费不可小于上级 " + belongPrice.getS0_qrJiejikaFj() + "元");
        }


        //银联二维码贷记卡结算
        if (targetPrice.getS0_qrDaijika().compareTo(belongPrice.getS0_qrDaijika()) == -1) {
            throw new BusinessException("银联二维码贷记卡结算不可小于上级 " + belongPrice.getS0_qrDaijika());
        }
        //银联二维码贷记卡附加手续费
        if (targetPrice.getS0_qrDaijikaFj().compareTo(belongPrice.getS0_qrDaijikaFj()) == -1) {
            throw new BusinessException("银联二维码贷记卡附加手续费不可小于上级 " + belongPrice.getS0_qrDaijikaFj() + "元");
        }






        //云闪付结算
        if (targetPrice.getYunshanfu0().compareTo(belongPrice.getYunshanfu0()) == -1) {
            throw new BusinessException("云闪付结算不可小于上级 " + belongPrice.getYunshanfu0());
        }
        //云闪付附加手续费
        if (targetPrice.getYunshanfu0Fj().compareTo(belongPrice.getYunshanfu0Fj()) == -1) {
            throw new BusinessException("云闪付附加手续费不可小于上级 " + belongPrice.getYunshanfu0Fj() + "元");
        }

        //微信结算
        if (targetPrice.getSaoma0Wx().compareTo(belongPrice.getSaoma0Wx()) == -1) {
            throw new BusinessException("微信结算不可小于上级 " + belongPrice.getSaoma0Wx());
        }
        //微信附加手续费
        if (targetPrice.getSaoma0FjWx().compareTo(belongPrice.getSaoma0FjWx()) == -1) {
            throw new BusinessException("微信附加手续费不可小于上级 " + belongPrice.getSaoma0FjWx() + "元");
        }

        //支付宝结算
        if (targetPrice.getSaoma0Zfb().compareTo(belongPrice.getSaoma0Zfb()) == -1) {
            throw new BusinessException("支付宝结算不可小于上级 " + belongPrice.getSaoma0Zfb());
        }
        //支付宝附加手续费
        if (targetPrice.getSaoma0FjZfb().compareTo(belongPrice.getSaoma0FjZfb()) == -1) {
            throw new BusinessException("支付宝附加手续费不可小于上级 " + belongPrice.getSaoma0FjZfb() + "元");
        }
    }


    public static void valiNextPrice(AgentPriceCommon target,AgentPriceCommon next){
        //刷卡借记卡结算
        if (target.getS0_shuakaJiejika().compareTo(next.getS0_shuakaJiejika()) == 1) {
            throw new BusinessException("刷卡借记卡结算不可大于下级 " + next.getS0_shuakaJiejika());
        }
        //刷卡借记卡封顶
        if (target.getS0_shuakaJiejikaFd().compareTo(next.getS0_shuakaJiejikaFd()) == 1) {
            throw new BusinessException("刷卡借记卡封顶不可大于下级 " + next.getS0_shuakaJiejikaFd());
        }
        //刷卡借记卡附加
        if(target.getS0_shuakaJiejikaFj() != null){
            if (target.getS0_shuakaJiejikaFj().compareTo(next.getS0_shuakaJiejikaFj()) == 1) {
                throw new BusinessException("刷卡借记卡附加手续费不可大于下级 " + next.getS0_shuakaJiejikaFj() + "元");
            }
        }

        //刷卡贷记卡结算
        if (target.getS0_shuakaDaijika().compareTo(next.getS0_shuakaDaijika()) == 1) {
            throw new BusinessException("刷卡贷记卡结算不可大于下级 " + next.getS0_shuakaDaijika());
        }
        if (target.getS0_shuakaDaijikaFj() != null) {
            //刷卡贷记卡附加手续费
            if (target.getS0_shuakaDaijikaFj().compareTo(next.getS0_shuakaDaijikaFj()) == 1) {
                throw new BusinessException("刷卡贷记卡附加手续费不可大于下级 " + next.getS0_shuakaDaijikaFj() + "元");
            }
        }

        //银联二维码借记卡结算
        if (target.getS0_qrJiejika().compareTo(next.getS0_qrJiejika()) == 1) {
            throw new BusinessException("银联二维码借记卡结算不可大于下级 " + next.getS0_qrJiejika());
        }
        //银联二维码借记卡封顶
        if (target.getS0_qrJiejikaFd().compareTo(next.getS0_qrJiejikaFd()) == 1) {
            throw new BusinessException("银联二维码借记卡封顶不可大于下级 " + next.getS0_qrJiejikaFd());
        }
        //银联二维码借记卡附加
        if(target.getS0_qrJiejikaFj() != null){
            if (target.getS0_qrJiejikaFj().compareTo(next.getS0_qrJiejikaFj()) == 1) {
                throw new BusinessException("银联二维码借记卡附加手续费不可大于下级 " + next.getS0_qrJiejikaFj() + "元");
            }
        }

        //银联二维码贷记卡结算
        if (target.getS0_qrDaijika().compareTo(next.getS0_qrDaijika()) == 1) {
            throw new BusinessException("银联二维码贷记卡结算不可大于下级 " + next.getS0_qrDaijika());
        }
        if (target.getS0_qrDaijikaFj() != null) {
            //银联二维码贷记卡附加手续费
            if (target.getS0_qrDaijikaFj().compareTo(next.getS0_qrDaijikaFj()) == 1) {
                throw new BusinessException("银联二维码贷记卡附加手续费不可大于下级 " + next.getS0_qrDaijikaFj() + "元");
            }
        }

        //云闪付结算
        if (target.getYunshanfu0().compareTo(next.getYunshanfu0()) == 1) {
            throw new BusinessException("云闪付结算不可大于下级 " + next.getYunshanfu0());
        }
        if (target.getYunshanfu0Fj() != null) {
            //云闪付附加手续费
            if (target.getYunshanfu0Fj().compareTo(next.getYunshanfu0Fj()) == 1) {
                throw new BusinessException("云闪付附加手续费不可大于下级 " + next.getYunshanfu0Fj() + "元");
            }
        }

        //微信结算
        if (target.getSaoma0Wx().compareTo(next.getSaoma0Wx()) == 1) {
            throw new BusinessException("微信结算不可大于下级 " + next.getSaoma0Wx());
        }
        if (target.getSaoma0FjWx() != null) {
            //微信附加手续费
            if (target.getSaoma0FjWx().compareTo(next.getSaoma0FjWx()) == 1) {
                throw new BusinessException("微信附加手续费不可大于下级 " + next.getSaoma0FjWx() + "元");
            }
        }

        //支付宝结算
        if (target.getSaoma0Zfb().compareTo(next.getSaoma0Zfb()) == 1) {
            throw new BusinessException("支付宝结算不可大于下级 " + next.getSaoma0Zfb());
        }
        if (target.getSaoma0FjZfb() != null) {
            //支付宝附加手续费
            if (target.getSaoma0FjZfb().compareTo(next.getSaoma0FjZfb()) == 1) {
                throw new BusinessException("支付宝附加手续费不可大于下级 " + next.getSaoma0FjZfb() + "元");
            }
        }
    }









}
